package com.cuz.daileetcode;

import com.cuz.daileetcode.utils.ListNode;

public class Day28_2 {


    /***
     * 找成环链表中的入环节点
     *
     * @param head
     * @return
     */
    public static ListNode findFirstLoopNode(ListNode head) {
        //单链表如果节点数小2 不可能相交
        if (head == null || head.next == null || head.next.next == null) {
            return null;
        }
        ListNode fast = head.next;
        ListNode slow = head;
        boolean isHasLoop = false;
        while (fast != null || fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                isHasLoop = true;
                break;
            }
        }
        if (!isHasLoop) {
            return null;
        } else {
            slow = head;
            while (slow != fast) {
                slow = slow.next;
                fast = fast.next;
            }
            return slow;
        }
    }
}
